{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Using Richardson Extrapolation with Finite Differences"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        "from math import sin, cos"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Here are a function and its derivative. We also choose a \"center\" about which we carry out our experiments:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "collapsed": true
      },
      "outputs": [],
      "source": [
        "f = sin\n",
        "df = cos\n",
        "\n",
        "x = 2.3"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We then compare the accuracy of:\n",
        "\n",
        "* First-order (right) differences\n",
        "* First-order (right) differences with half the step size\n",
        "* An estimate based on these two using Richardson extrapolation\n",
        "\n",
        "against `true`, the actual derivative"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Err FD1: 0.08581\tErr FD: 0.0448122\tErr Rich: 0.00381441\n",
            "Err FD1: 0.0448122\tErr FD: 0.022862\tErr Rich: 0.000911846\n",
            "Err FD1: 0.022862\tErr FD: 0.0115423\tErr Rich: 0.000222501\n",
            "Err FD1: 0.0115423\tErr FD: 0.00579859\tErr Rich: 5.49282e-05\n",
            "Err FD1: 0.00579859\tErr FD: 0.00290612\tErr Rich: 1.3644e-05\n",
            "Err FD1: 0.00290612\tErr FD: 0.00145476\tErr Rich: 3.39995e-06\n",
            "Err FD1: 0.00145476\tErr FD: 0.000727804\tErr Rich: 8.48602e-07\n"
          ]
        }
      ],
      "source": [
        "for k in range(3, 10):\n",
        "    h = 2**(-k)\n",
        "\n",
        "    fd1 = (f(x+2*h) - f(x))/(2*h)\n",
        "    fd2 = (f(x+h) - f(x))/h\n",
        "    \n",
        "    richardson = (-1)*fd1 + 2*fd2\n",
        "    \n",
        "    true = df(x)\n",
        "    \n",
        "    print(\"Err FD1: %g\\tErr FD: %g\\tErr Rich: %g\" % (\n",
        "            abs(true-fd1),\n",
        "            abs(true-fd2),    \n",
        "            abs(true-richardson)))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 1
}